use  	"$data\4_individual_ano_reg_2.dta", clear

mean 	mwage_ts, over(assignment)
bys 	id: egen mwage_ts_firm = sum(mwage_ts)
replace mwage_ts_firm = . if  mwage_ts_firm==0

by		id: egen mwage_ts_firm_2021 =sum(mwage_ts_2021)
replace mwage_ts_firm_2021 = 0 if  mwage_ts_firm_2021==.

by 		id: egen firm_size_emp = count(id_indiv)

gen 	mwage_ts_firm_pc = mwage_ts_firm / firm_size_emp
gen 	mwage_ts_firm_pc_2021 = mwage_ts_firm_2021 / firm_size_emp



	foreach var of varlist mwage_ts_firm mwage_ts_firm_pc {
		
			rename `var'_2021 `var'_raw_2021
						
				* Standardise LDV for different samples 
				egen `var'_2021=std(`var'_raw_2021)
				replace `var'_2021=0 if missing(`var'_raw_2021) 
				
				gen missing_`var'_2021=1 if missing(`var'_raw_2021)
				replace missing_`var'_2021=0 if !missing(`var'_raw_2021)
			}

	global outcomes  mwage_ts mwage_ts_firm_pc

merge m:1 id wave using "$data\2_firm_regressions_individual_paper.dta", keepusing(empcat broad_sector ag_ano sh_aminwage*)
keep if _merge == 3

* Panel A: Main Outcomes (ITT)
	eststo clear
		foreach o of global outcomes {
			local d=`c'+1
			eststo m`c'`x': areg `o' assignment `o'_2021 $controls_balance  missing_`o'_2021 i.wave $se_indiv
			qui summ `o' if assignment==0
			local mu : di %5.2f r(mean)
			estadd local mu `mu'
		
		}
	


duplicates drop id, force


estimates clear
foreach o of varlist mwage_ts_firm mwage_ts_firm_pc {
	eststo est_1_`o': areg `o' assignment `o'_2021 i.broad_sector i.ag_ano  missing_`o'_2021 i.wave  , a(strata_all_coll) r
	summ `o' if assignment==0
	qui summ `o' if assignment==0
			local mu : di %5.2f r(mean)
			estadd local mu `mu'
			}

label var assignment "ITT"
esttab est_1_* using "$results\01_tables\Table_S12_results_wagebill.tex", replace ///
    keep(assignment) ///
    b(%5.2f) se(%5.2f) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    scalars("r2 R2" "mu Mean") ///
    mtitle("Wage Bill" "Wage Bill p.w.") 
	
	esttab est_1_* , replace ///
    keep(assignment) ///
    b(%5.2f) se(%5.2f) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    scalars("r2 R2" "mu Mean") ///
    mtitle("Wage Bill" "Wage Bill p.w.") 